package com.wjc.socket5webkit;

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.webkit.SslErrorHandler;
import android.net.Uri;
import android.net.http.SslError;


public class Socket5WebkitActivity extends Activity {
	/** Called when the activity is first created. */

	private Button button;
	WebView web;
	EditText text;

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
       
       web=(WebView)this.findViewById(R.id.webView1);
       text=(EditText)this.findViewById(R.id.editText1);
       text.setText("https://www.gmail.com/");
    
       button=(Button)this.findViewById(R.id.button1);
       web.requestFocus();
       button.setOnClickListener(new OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//web.setVisibility(View.VISIBLE);
			
			web.loadUrl(text.getText().toString());
			 web.requestFocus();
		}}
       );
        //Socket5ConnectTcp t=new Socket5ConnectTcp();
       // t.test();
       //重启服务
       ((Button)findViewById(R.id.button2)).setOnClickListener( 
    		   new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					stopService(new Intent("com.wjc.HTTP_SERVICE"));
					startService(new Intent("com.wjc.HTTP_SERVICE"));
				}
    	   
       }
       );
       //停止服务
       ((Button)findViewById(R.id.button3)).setOnClickListener( 
    		   new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					stopService(new Intent("com.wjc.HTTP_SERVICE"));
					
				}
    	   
       }
       );
        Log.d("tt", "here");
        
    	WebSettings webSettings=web.getSettings();
		webSettings.setJavaScriptEnabled(true);
		//设置可以访问文件
		webSettings.setAllowFileAccess(true);
		//设置支持缩放
		webSettings.setBuiltInZoomControls(true);
		web.setDownloadListener(new DownloadListener() {
		                        public void onDownloadStart(String url, String userAgent,
		                                        String contentDisposition, String mimetype,
		                                        long contentLength) {
		                                //实现下载的代码
		                                                      //Uri uri = Uri.parse("http://10.10.10.173:8080/1/1.apk");
		                        	Uri uri=Uri.parse(url); 
		                       Intent intent = new Intent(Intent.ACTION_VIEW,uri);
		                                startActivity(intent);
		                        }
		                });
		web.setWebViewClient(new WebViewClient(){
			public boolean shouldOverrideUrlLoading(WebView view,String url){
				web.clearDisappearingChildren();
				web.loadUrl(url);
				
				return true;
			}
			@Override
			public void onPageFinished(WebView view,String url)
			{
				super.onPageFinished(view,url);
			}

			 public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
			                //handler.cancel(); // Android默认的处理方式
				 Log.d("sslError", "msg"+error.getPrimaryError());
			                handler.proceed();  // 接受所有网站的证书
			                //handleMessage(Message msg); // 进行其他处理
			                

			            }

		});
		
	
        
    }

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			if(web.canGoBack()){
				web.goBack();
			}else{
				startActivity(new Intent(Socket5WebkitActivity.this,StartPageActivity.class));
				finish();
			}
		}
		return false;
	}

}